<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            display: fiex;
            justify-content: space-around;
            padding: 10px;
        }
        li img {
            width: 100px;
        }
    </style>
    <script src="../../lib/vue.js"></script>
</head>
<body>
    <div id="box">
        <div v-if="list.length === 0">购物车空空</div>
        <template v-else>
            <input type="checkbox" @change="handleChange" v-model="isAllChecked">全选/全不选
            <ul>
                <li v-for="item in list" :key="item.id">
                    <input type="checkbox" v-model="checkGroup" :value="item">
                    <img :src="item.pic">
                        <div>商品名：{{item.name}}</div>
                        <div>商品价格：{{item.price}}</div>
                    <div>
                        <button @click="item.number--" :disabled="item.number <= 0">-</button>
                        {{item.number}}
                        <button @click="item.number++" :disabled="item.number === item.limit">+</button>
                    </div>
                    <div >
                        <button @click="handleDelete(item.id)">删除</button>
                    </div>
                </li>
            </ul>

            <div style="background-color:rgb(124, 95, 115)">总金额:{{sum()}}</div>
        </template>
        
    </div>

    <script>
        var vm = new Vue({
            el: "#box",
            data: {
                list: [
                    {
                        name: "商品1",
                        price: 10,
                        number: 1,
                        id: 1,
                        limit: 10,
                        pic: "https://pic2.zhimg.com/80/v2-0716ab15aa8f6ce0d9a120265342f669_720w.jpg"
                    },
                    {
                        name: "商品2",
                        price: 100,
                        number: 2,
                        id: 2,
                        limit: 10,
                        pic: "https://pic2.zhimg.com/80/v2-0716ab15aa8f6ce0d9a120265342f669_720w.jpg"
                    }
                ],
                checkGroup: [],
                count: 0,
                isAllChecked: false
            },
            methods: {
                sum() {
                    //每次的checkGroup改变都会调用这个函数
                    let total = 0;
                    this.checkGroup.forEach(item => {
                        total += item.price * item.number
                    })
                    this.count = total
                    return total
                },
                handleDelete(id) {
                    console.log(id);
                    this.list = this.list.filter(item => item.id !== id)
                    //删除checkGroup对应的元素
                    this.checkGroup = this.checkGroup.filter(item =>
                        item.id !== id
                    )
                },
                handleChange(ev) {
                    // 绑定change函数不用加括号
                    // 怎么得到是否勾选，可以通过ev拿到，可也以绑定v-model绑定一个bool值
                    console.log("全选/全不选" + ev.target.checked + this.isAllChecked)
                    if(this.isAllChecked) {
                        this.checkGroup = this.list;
                    }else {
                        this.checkGroup = [];
                    }
                }
            }
        })
    </script>
</body>
</html>